import mongoose = require("mongoose");
import { GlobalData } from "./GlobalData";

//interfaces
import { IUser } from "./interfaces/IUser";

//models
import { IModel } from "./models/IModel";
import { IUserModel } from "./models/IUserModel";

//schemas
import { UserSchema } from "./schemas/UserSchema";


export class Main
{
	private db:mongoose.Connection;

	public constructor()
	{
		console.warn(1111111);
		this.init();
	}

	private init(): void
	{
		this.db = mongoose.createConnection('mongodb://47.92.111.83:27017/heros');
		this.db.on('error', console.error.bind(console, '连接错误:'));
		let self = this;
		this.db.once('open', function ()
		{
			console.log("mongo connected...");
			GlobalData.isDBConnected = true;
			console.warn(GlobalData.isDBConnected);
			// self.dbConnected();
			let model = self.db.model<IUserModel>("Person",UserSchema);
			let user: IUser = {
				email: "foo@bar.com",
				firstName: "Brian",
				lastName: "Love"
			};
			new model(user).save();
		});
		console.warn(GlobalData.isDBConnected);
	}

	private dbConnected(): void
	{
		console.log("dbConnected...");
		let model = this.db.model<IUserModel>("Person",UserSchema);
		let user: IUser = {
			email: "foo@bar.com",
			firstName: "Brian",
			lastName: "Love"
		};
		new model(user).save();
	}
}
